<?php
namespace app\index\controller;
use app\index\controller\BaseController;
use think\Db;

class MemberController  extends BaseController {

	protected function _initialize() {
		parent::_initialize();
	}

	/**
	 * 会员中心
	 */
	public function index() {
		//获取用户信息
		$user_info = db('member')->where('user_id',session('user_id'))->find();
		//获取用户订单信息
		$order_list = db('good_order')->where('user_id',$user_info['user_id'])->select();
		$d_fk=0;
		$d_fh=0;
		$d_sh=0;
		$d_pj=0;
		foreach($order_list as $key=>$valus){
			
			if($valus['status'] ==1){
				 $d_fk++;
			}elseif($valus['status'] ==2){
				$d_fh++;
			}elseif($valus['status'] ==3){
				$d_sh++;
			}elseif($valus['status'] ==4){
				$d_pj++;
			}
		}
		$data['d_fk'] = $d_fk;
		$data['d_fh'] = $d_fh;
		$data['d_sh'] = $d_sh;
		$data['d_pj'] = $d_pj;
		
		
		
		$this->assign('data', $data);
		$this->assign('user_info', $user_info);
		return $this->fetch();
	}

	/**
	 * 修改资料
	 */
	public function update() {
		if ($this->request->isPost()) {
			$data = $this->request->post();
			unset($data['file']);
			$result = db('member')->where('user_id', session('user_id'))->update($data);
			if ($result) {
				$this->success('保存成功');
			} else {
				$this->error('保存失败');
			}
		} else {
			$model = db('member')->find(session('user_id'));
			$this->assign('model', $model);
			return $this->fetch();
		}

	}

	

	//意见反馈
	function feedback() {
		if ($this->request->isPost()) {
			$data = $this->request->post();
			$data['msg_time'] = now_time();
			$data['user_id'] = session('user_id');
			$result = db('sys_feedback')->insert($data);
			if($result) {
				return  json(["code"=>200,"msg"=>"提交成功",'url'=>url('member/index')]);
			} else {
				return  json(["code"=>403,"msg"=>"提交失败"]);
			}
		} else {
			return $this->fetch();
		}
	}
	
	public function address() {
		$address_list =db('member_address')
						->where('user_id',session('user_id'))
						->select();
		$this->assign('address_list', $address_list);
		return $this->fetch();
	}
	
	
	function address_add() {
		if(request()->isPost()) {
			$data = input('post.');
			
			$result = true;
			Db::startTrans();
			//检查用户是否有地址
			$address = db('member_address')->where('user_id',session('user_id'))->find();
			if(!$address){
				$data['is_default'] = 1;
			}else if(!isset($data['is_default'])){
				
				$rest1 = db('member_address')
						->where('user_id', session('user_id'))
						->update(['is_default' => 0]);
						
				if(!$rest1) $result=false;
				$data['is_default'] = 1;
			}
			
			$data['user_id'] = session('user_id');
			$data['add_time'] = now_time();
			$rest2 = db('member_address')->insert($data);
			if(!$rest2) $result=false;
			
			if($result){
				Db::commit();
				return  json(["code"=>200,"msg"=>"添加成功",'url'=>url('member/address')]);
			}else{
				Db::rollback();
				return  json(["code"=>403,"msg"=>"添加失败"]);
			}
		}
		
		return $this->fetch();
	}
	
	function address_default(){
		if(request()->isPost()) {
			$address_id = input('address_id');
			
			if (!is_numeric($address_id)) {
				return  json(["code"=>405,"msg"=>"数据异常"]);
			}
			
			$rest1 = db('member_address')
						->where('user_id', session('user_id'))
						->update(['is_default' => 0]);
			if($rest1){
				$rest2 = db('member_address')
						->where('id', $address_id)
						->where('user_id', session('user_id'))
						->update(['is_default' => 1]);
				if($rest2){
					return  json(["code"=>200,"msg"=>"修改成功"]);
				}else{
					return  json(["code"=>403,"msg"=>"修改失败"]);
				}
			}else{
				return  json(["code"=>405,"msg"=>"修改失败"]);
			}
		}
	}
	
	
	
	
	function address_update() {
		$address_id = input('address_id');
		if (!is_numeric($address_id)) {
			return  json(["code"=>405,"msg"=>"数据异常"]);
		}
		$address_info = db('member_address')->where('id', $address_id)->where('user_id', session('user_id'))->find();

		$this->assign('address_info',$address_info);
		
		if(request()->isPost()) {
			$data = input('post.');
			unset($data['address_id']);
			$result = db('member_address')
						->where('id', $address_id)
						->where('user_id', session('user_id'))
						->update($data);
			
			if($result) {
				return  json(["code"=>200,"msg"=>"修改成功",'url'=>url('member/address')]);
			} else {
				return  json(["code"=>403,"msg"=>"修改失败"]);
			}
		}
		
		
		
		return $this->fetch();
	}
	function address_del() {
		if(request()->isPost()) {
			$address_id = input('address_id');
			
			if (!is_numeric($address_id)) {
				return  json(["code"=>405,"msg"=>"数据异常"]);
			}
			
			$result = db('member_address')->where('id', $address_id)->where('user_id', session('user_id'))->delete();
			
			if($result) {
				return  json(["code"=>200,"msg"=>"删除成功"]);
			} else {
				return  json(["code"=>403,"msg"=>"删除失败"]);
			}
		}
	}
	
	public function yue_record() {
		$data =db('member_charge')
						->where('user_id',session('user_id'))
						->sum('balance');
		$this->assign('data', $data);	
		$list =db('member_charge')
						->where('user_id',session('user_id'))
						->order('add_time desc')
						->select();
		$this->assign('list', $list);
		return $this->fetch();
	}
	public function gongyi_record() {
		$data =db('member_gongyi')
						->where('user_id',session('user_id'))
						->sum('balance');
		$this->assign('data', $data);	
		$list =db('member_gongyi')
						->where('user_id',session('user_id'))
						->select();
		$this->assign('list', $list);
		return $this->fetch();
	}
	
	public function about() {
		return $this->fetch();
	}
	public function invite() {
		
		$data= db('member')->where('user_id', session('user_id'))->find();
		$this->assign('data',$data);
		$list= db('member')->where('pid1', session('user_id'))->select();
		$this->assign('list',$list);
		return $this->fetch();
	}
	public function edit_password() {
		if(request()->isPost()){
			$xpassword['password']=md5_salt(input('xpassword'));
			$jpassword['password']=md5_salt(input('jpassword'));
			$user_info = db('member')->where('user_id',session('user_id'))->where($jpassword)->find();
			if($user_info){
				$rest = db('member')->where('user_id', session('user_id'))->update($xpassword);
				if($rest){
					return json(['code'=>200,'msg'=>'修改成功','url'=>'/index/index/login']);
				}else{
					return json(['code'=>405,'msg'=>'修改失败']);
				}
			}else{
				return json(['code'=>403,'msg'=>'旧密码不对']);
			}
		}
		return $this->fetch();
	}
	public function orders() {
		
		$where['user_id'] = session('user_id');
		if(input('status')>0){
			$where['status'] = input('status');
		}else{
			$where['status'] = ['<>',8];
		}
		
		//获取用户订单信息
		$order_list = db('good_order')
						->where($where)
						->order('add_time desc')
						->select();
						
		foreach($order_list as $key=>&$value){
			//获取订单详情
			$order_item = db('order_items')->where('order_id',$value['id'])->select();
			$value['order_item'] = $order_item;
		}
		$this->assign('status',input('status'));
		$this->assign('order_list',$order_list);
		return $this->fetch();
	}
	
	
}
